import {Module} from 'vuex'
import { IProductState } from "./types";
import { getPageListData } from "../../../service/main/product/product";
const productModule: Module<IProductState, any> = {
  namespaced: true,
  state() {
    return {
      productList: [],
      productCount: 0,
    }
  },
  getters:{
    productListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    productListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  mutations:{
    changeGoodsList(state, productList: any[]) {
      state.productList = productList
    },
    changeGoodsCount(state, productCount: number) {
      state.productCount = productCount
    }
  },
  actions:{
    async getPageListAction({ commit }, payload: any){
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`

      // 1.对页面发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageResult.data
      const changePageName =
      pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`change${changePageName}List`, list)
      commit(`change${changePageName}Count`, totalCount)
    }
  }
}
export default productModule